<!DOCTYPE html>
<html>

<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <meta http-equiv="X-UA-Compatible" content="IE=edge,chrome=1" />
    <title>新增仓库调拨</title>
    <link rel="stylesheet" href="../../../../../public/dist/css/layui.css">
    <link rel="stylesheet" href="../../../../../css/twocss/xingzenm.css">
</head>

<body>
    <div class="select common">
        <form class="layui-form" action="">
            <div class="select-title" style="height:40px;line-height:40px;">
                基本信息
            </div>
            <div class="basis-h">
                <div class="mess-pronew">

                    <div class="layui-form-item">
                        <div class="layui-inline">
                            <label class="layui-form-label" style="width:95px;">调拨日期:</label>
                            <div class="layui-input-inline">
                                <input type="text" class="layui-input" id="temis">
                            </div>
                        </div>
                    </div>


                    <div class="layui-form-item">
                        <div class="layui-inline ">
                            <label class="layui-form-label" style="width:95px;">调出仓库:</label>
                            <div class="layui-input-inline ">
                                <select lay-filter="warehousess" class="warehouses">
                                     <option value="">---请选择---</option>
                                </select>
                            </div>
                        </div>
                    </div>
                </div>

                <div class="mess-pronew">

                    <div class="layui-form-item">
                        <div class="layui-inline">
                            <label class="layui-form-label" style="width:95px;">负责人:</label>
                            <div class="layui-input-inline">
                                <select lay-filter="heads" class="headpeople">
                                     <option value="">---请选择---</option>
                                </select>
                            </div>
                        </div>
                    </div>


                    <div class="layui-form-item">
                        <div class="layui-inline">
                            <label class="layui-form-label" style="width:95px;">调出二级仓库:</label>
                            <div class="layui-input-inline">
                                <select lay-filter="twowarehousess" class="twowarehouses">
                                     <option value="">---请选择---</option>
                                </select>
                            </div>
                        </div>
                    </div>


                </div>
            </div>
            <div class="select-title" style="height:30px;line-height:44px;margin-top: 10px;border-bottom-color:#fff;">
                仓库调拨
            </div>
        </form>

        <div style="margin:10px 0;">
            <button type="button" class="layui-btn   chooses"><i class="layui-icon">&#xe608;</i> 选择调拨品</button>
            <button type="button" class="layui-btn   deleteaa"> <i class="layui-icon">&#xe640;</i>删除</button>

            <div style="width: 100%;margin:10px 0;" class="wdacon">

            </div>
        </div>


        <div class="layui-btn-group" style="width: 100%;display: flex;justify-content: center;margin-top: 10px;">
            <button class="layui-btn  saves"><i class="layui-icon">&#xe674;</i>保存</button>
            <button class="layui-btn huifan"><i class="layui-icon">&#xe65c;</i>返回</button>
        </div>
    </div>

</body>
<script src="../../../../../js/module/jquery-3.4.1.min.js"></script>
<script src="../../../../../public/dist/layui.js" charset="utf-8"></script>
<script src="../../../../../js/common/api.js"></script>
<script src="../../../../../js/common/template-web.js"></script>

</html>

<script type="text/html" id="contr">
    <table lay-filter="demo" style="width: 100%;">
        <thead>
            <tr class="xingsd">
                <th>编号</th>
                <th>名称</th>
                <th>批次</th>
                <th>库存数量</th>
                <th>库存重量(kg)</th>
                <th>调入仓库</th>
                <th>调入二级仓库</th>
                <th>调出数量</th>
                <th>调出重量(kg)</th>
            </tr>
        </thead>
        <tbody class="datalang">
            {{each listee}}
            <tr class="trsd">
                <td style="text-align: center;">{{$index}} </td>
                <td>{{$value.productname}}</td>
                <td>{{$value.batch}}</td>
                <td style="text-align: center;">{{$value.stocknum}}</td>
                <td style="text-align: center;">{{$value.weight}}</td>
                <td>{{$value.inpstorehouse}}</td>
                <td style="text-align: center;">{{$value.inparea}}</td>
                <td style="text-align: center;">{{$value.num}}</td>
                <td style="text-align: center;">{{$value.weights}}</td>
            </tr>
            {{/each}}
        </tbody>
    </table>
</script>


<!-- 查看的内容 -->
<script type="text/html" id="contentquery">
    <div class="tiaojian">
        <p>查询条件</p>
        <div class="conditions">
            <div>
                <label>名称 :</label>
                <input type="text" class="ming">
            </div>

            <div>
                <label>批次 :</label>
                <input type="text" class="dans">
            </div>

            <div>
                <button type="button" class="layui-btn  layui-btn-xs" id="xhaxun">查询</button>
                <button type="button" class="layui-btn  layui-btn-xs" id="zhongzhiess">重置</button>
            </div>
        </div>
    </div>
    <div class="doeas">
        <table class="layui-hide " id="demodemo" lay-data="{id: 'idTest'}" lay-filter="testdome"></table>
    </div>

    <div style="display:flex;justify-content:center;">
        <button type="button" class="layui-btn  layui-btn-sm" id="dingque">确定</button>
        <button type="button" class="layui-btn  layui-btn-sm" id="quexiaos">取消</button>
    </div>

</script>
<script>
    layui.use(['table', "layer", "form", "laydate", "util"], function() {
        var $ = layui.jquery,
            table = layui.table,
            layer = layui.layer,
            laydate = layui.laydate,
            form = layui.form,
            projectobj = {},
            obj = {},
            listds = [],
            util = layui.util;



        //时间选择    
        laydate.render({
            elem: '#temis',
            show: true,
            done: function(value) {
                var batch = value
                projectobj.transferdate = batch + " " + "00:00:00";
            }
        });


        //负责人添加
        noAjax('user/getUser', {}, "GET").done(function(resp) {
            $('.headpeople').append(new Option(resp.realName, resp.realName)); // 下拉菜单里添加元素
            layui.form.render("select");
        }).fail(function(err) {
            console.log(err)
        });


        //负责人的获取
        form.on('select(heads)', function(data) {

            return
        })


        //一级仓库
        var dataresps = null;
        noAjax('OrdProcessing/getBasStorage', {}, "GET").done(function(resp) {
            dataresps = resp;
            console.log(resp)
            $.each(resp.list, function(index, item) {
                $('.warehouses').append(new Option(item.storagename, item.id));
            });
            layui.form.render("select");
            return;
        }).fail(function(err) {
            console.log(err)
        });


        //一级选出二级仓库
        form.on('select(warehousess)', function(data) {
            projectobj.outstorageid = data.value;
            var elees = $(this).text();
            projectobj.outstorehouse = elees;
            $(".twowarehouses").empty();
            $('.twowarehouses').append(new Option("---请选择---", ""));
            $.each(dataresps.list, function(index, item) {
                if (item.childNodes != null) {
                    if (item.childNodes.length > 0 && elees == item.storagename) {
                        $.each(item.childNodes, function(i, items) {
                            $('.twowarehouses').append(new Option(items.storagename, items.id));
                        })
                    }
                }
            });
            layui.form.render("select");
            return
        })



        //二级仓库
        form.on('select(twowarehousess)', function(data) {
            projectobj.outareaid = data.value;
            projectobj.outarea = $(this).text();
            return
        })


        // 选择调拨品
        $(".chooses").click(function() {

            if ($(".twowarehouses").children().length <= 1 || $(".twowarehouses").val() == "") {
                alerts("请选择调出二级仓库!")
                return
            }
            layer.open({
                type: 1,
                title: "查看",
                shadeClose: true, //是否点击遮罩关闭
                anim: 5, //弹出动画
                scrollbar: false, //窗口外滚动条是否出现
                skin: "layui-layer-rim", //加上边框
                area: ["760px", "500px"], //宽高
                content: '<div class="ssss"></div>',
                success: function(layero, index) {

                    var html = template("contentquery", "");
                    $(".ssss").html(html);
                    transfers();
                    //取消
                    $("#quexiaos").click(function() {
                        layer.closeAll();
                    })



                    //确定
                    $("#dingque").click(function() {
                        var checkStatus = table.checkStatus('idTest');
                        if (checkStatus.data.length == 0) {
                            alerts("请选择!");
                            return
                        }
                        listds = checkStatus.data;
                        layer.closeAll();

                        $.each(listds, function(v, items) {
                            if (items.inpstorageid == undefined) {
                                listds.splice(v, 1);
                            }
                        })
                        var ssd = {};
                        console.log(listds);
                        ssd.listee = listds;
                        var htmls = template("contr", ssd);
                        $(".wdacon").html(htmls);
                        return
                    })
                    return
                }
            });
        })


        function transfers() {
            //查询
            $("#xhaxun").click(function() {
                if ($(".ming").val() == "" && $(".dans").val() == "") {
                    layer.msg("请输入查询条件！", {
                        time: 1000,
                        offset: "t"
                    });
                    return;
                }
                var datelist = {
                    batch: $(".dans").val(),
                    productname: $(".ming").val(),
                    areaid: $(".twowarehouses").val()
                };
                tableIns.reload({
                    where: datelist,
                    request: {
                        pageName: "currentPage", //页码的参数名称，默认：page
                        limitName: "pageSize" //每页数据量的参数名，默认：limit
                    },
                    page: {
                        curr: 1, //重新从第 1 页开始
                        layout: ["prev", "page", "next", "skip", "count"]
                    }
                });

            })

            //重置
            $("#zhongzhiess").click(function() {
                $(".ming").val("");
                $(".dans").val("");
                tableIns.reload({
                    where: {
                        batch: "",
                        productname: "",
                        areaid: $(".twowarehouses").val()
                    },
                    page: {
                        curr: 1, //重新从第 1 页开始
                        layout: ["prev", "page", "next", "skip", "count"]
                    }
                });
            })


            var table = layui.table;
            var tableIns = table.render({
                elem: "#demodemo",
                id: 'idTest',
                url: base + "StoInventory/getStoInventory",
                method: "GET",
                where: {
                    batch: "",
                    productname: "",
                    areaid: $(".twowarehouses").val()
                },
                headers: {
                    Authorization: "Bearer" + " " + sessions
                },
                request: {
                    pageName: "currentPage",
                    limitName: "pageSize"
                },
                limits: [10, 20],
                parseData: function(res) {
                    //res 即为原始返回的数据
                    console.log(res);
                    return {
                        code: res.code, //解析接口状态
                        msg: res.message, //解析提示文本
                        totalNum: res.pageBean.totalNum, //解析数据长度
                        lists: res.pageBean.lists //解析数据列表
                    };
                },
                response: {
                    statusName: "code", //数据状态的字段名称，默认：code
                    statusCode: 10000, //成功的状态码，默认：0
                    msgName: "message", //状态信息的字段名称，默认：msg
                    countName: "totalNum", //数据总数的字段名称，默认：count
                    dataName: "lists" //数据列表的字段名称，默认：data
                },
                cellMinWidth: 80,
                page: {
                    //支持传入 laypage 组件的所有参数（某些参数除外，如：jump/elem） - 详见文档
                    layout: ["prev", "page", "next", "skip", "count"], //自定义分页布局
                    //,curr: 5 //设定初始在第 5 页
                    groups: 5, //只显示 1 个连续页码
                    first: true, //不显示首页
                    last: true, //不显示尾页
                    prev: "下一页",
                    next: "上一页",
                    theme: "#c81623"
                },
                done: function() {

                    //一级仓库
                    var dsud = null;
                    noAjax('OrdProcessing/getBasStorage', {}, "GET").done(function(resp) {
                        console.log(resp)
                        dsud = resp.list;
                        $.each(resp.list, function(index, item) {
                            $('.chuyis').append(new Option(item.storagename, item.id));
                        });
                        layui.form.render("select");
                        return;
                    }).fail(function(err) {
                        console.log(err)
                    });


                    //一级选择二级
                    $('.chuyis').change(function() {
                        console.log(table.cache.idTest)
                        var indesss = $(this).parents("td").prev().children().text();
                        var elems = $(this).val();
                        var teles = $(this).children("option:selected").text();
                        $.each(table.cache.idTest, function(v, irs) {
                            if (indesss == irs.id) {
                                console.log(irs)
                                irs.inpstorageid = elems;
                                irs.inpstorehouse = teles;
                            }
                        })
                        console.log($(this).parents("td").prev().children().text())
                        $(this).parents("td").next().children().children(".chuer").empty();
                        $(this).parents("td").next().children().children(".chuer").append(new Option("---请选择---", ""));
                        var stss = $(this);
                        $.each(dsud, function(index, item) {
                            if (item.childNodes != null) {
                                if (item.childNodes.length > 0 && elems == item.id) {
                                    $.each(item.childNodes, function(i, items) {
                                        stss.parents("td").next().children().children(".chuer").append(new Option(items.storagename, items.id));
                                    })
                                }
                            }
                        });
                    })


                    //二级选择
                    $(".chuer").change(function() {
                        console.log(table.cache.idTest)
                        var inder = $(this).parents("td").prev().prev().children().text();
                        var eleer = $(this).val();
                        var teer = $(this).children("option:selected").text();
                        $.each(table.cache.idTest, function(v, its) {
                            if (inder == its.id) {
                                its.inpareaid = eleer;
                                its.inparea = teer;
                            }
                        })
                    })


                    //数量
                    $(".shul").blur(function() {
                        var shu = $(this).parents("td").prev().prev().prev().children().text();
                        var eleershu = $(this).val();
                        $.each(table.cache.idTest, function(v, its) {
                            if (shu == its.id) {
                                its.num = eleershu;
                            }
                        })
                    })

                    //重量
                    $(".zhongl").blur(function() {
                        var shus = $(this).parents("td").prev().prev().prev().prev().children().text();
                        var eleershul = $(this).val();
                        $.each(table.cache.idTest, function(v, its) {
                            if (shus == its.id) {
                                its.weights = eleershul;
                            }
                        })
                    })

                },
                cols: [
                    [{
                        type: 'checkbox',
                        fixed: 'left'
                    }, {
                        align: "center",
                        field: "productname",
                        title: "名称",
                        minWidth: 120
                    }, {
                        field: "batch",
                        title: "批次",
                        align: "center",
                        minWidth: 100
                    }, {
                        field: "stocknum",
                        title: "库存数量",
                        minWidth: 100,
                        align: "center",
                        templet: function(d) {
                            var jj = d.stocknum + " " + "袋"
                            return '<div  class="shus">' + jj + '</div>'
                        },
                    }, {
                        field: "weight",
                        title: "库存重量(kg)",
                        minWidth: 110,
                        align: "center",
                        templet: function(d) {
                            var jjs = d.weight
                            return '<div  class="zhongs">' + jjs + '</div>'
                        },
                    }, {
                        field: "id",
                        title: "id",
                        minWidth: 110,
                        align: "center",
                        hide: true
                    }, {
                        field: "measureaddr",
                        title: "调出仓库",
                        minWidth: 180,
                        align: "center",
                        event: "ckyij",
                        templet: function(d) {
                            return '<select  lay-ignore    name="dictName"   class="chuyis"    lay-filter="chuyi"> <option value="">---请选择---</option></select>'
                        },
                    }, {
                        field: "reason",
                        title: "调出二级仓库",
                        minWidth: 180,
                        align: "center",
                        templet: function(d) {
                            return '<select  lay-ignore   class="chuer"  lay-filter="chuer"   > <option value="">---请选择---</option></select>'
                        },
                    }, {
                        field: "reason",
                        title: "调出数量",
                        minWidth: 180,
                        align: "center",
                        templet: function(d) {
                            if (d.stocknum > 0) {
                                return '<input  type="number"   value=""   class="shul"  onblur="onblurs(' + d.stocknum + ',this)">'
                            } else {
                                return ""
                            }
                        },
                    }, {
                        field: "reason",
                        title: "调出重量(kg)",
                        minWidth: 180,
                        align: "center",
                        templet: function(d) {
                            if (d.weight > 0) {
                                return '<input  type="number"  class="zhongl"  onblur="onblurw(' + d.weight + ',this)">'
                            } else {
                                return ""
                            }
                        },
                    }]
                ]
            });
        }




        //保存
        $(".saves").click(function() {

            if ($("#temis").val() == "") {
                alerts("请选择时间！")
                return
            }

            if ($(".headpeople").val() == "") {
                alerts("请选择负责人！")
                return
            }

            if ($(".warehouses").val() == "") {
                alerts("请选择调出仓库！")
                return
            }
            if ($(".twowarehouses").val() == "") {
                alerts("请调出二级仓库！")
                return
            }
            if (listds.length < 1) {
                alerts("请选择调拨品！");
                return
            }

            $.each(listds, function(v, items) {
                items.productid = items.purchaseid;
                delete items.purchaseid;
                items.batchcode = items.batch;
                delete items.batch;
                items.weight = items.weights;
                delete items.weights;
                delete items.islock;
                delete items.producttype;
                delete items.stocknum;
                projectobj.transfername = items.productname;
            })

            projectobj.transferdate = $("#temis").val() + " " + "00:00:00";
            projectobj.stoOrderdetails = listds;
            projectobj = JSON.stringify(projectobj);
            handleAjax("StoInventory/addStoOrder", projectobj, "POST", "utf-8").done(function(resp) {
                layer.msg('操作成功', {
                    icon: 1,
                    time: 1000
                });
                setTimeout(function() {
                    window.location.href = "../transfers.html";
                }, 1500)
                return
            }).fail(function(err) {
                console.log(err)
            });
            return
        })



        //返回
        $(".huifan").click(function() {
            window.location.href = "../transfers.html";
        })



    })
</script>

<script>
    function onblurs(t, s) {
        if (Number(s.value) > Number(t)) {
            alerts("输入值大于库存数量");
            s.value = ""
        }
    }

    function onblurw(t, s) {
        if (Number(s.value) > Number(t)) {
            alerts("输入值大于库存重量")
            s.value = ""
        }
    }
</script>